# 导包
import unittest
from interfaceDatabase.api.login import LoginAPI
import json
from parameterized import parameterized
from interfaceDatabase import app
# 构造测试数据,基于json文件实现参数化
def build_data():
    json_file = app.BASE_DIR+"/data/login.json"
    test_data = []
    with open(json_file, encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            status_code = case_data.get("status_code")
            message = case_data.get("message")
            code = case_data.get("code")
            test_data.append((username, password, status_code, message, code))
        print("test_data=".format(username, password, status_code, message, code))
    return test_data


# 定义测试类
class TestLoginAPI(unittest.TestCase):
    # 前置处理
    def setUp(self):
        self.login_api = LoginAPI()

    # 后置处理
    def tearDown(self):
        print("end")

    # 定义测试方法
    # 登录成功
    @parameterized.expand(build_data())
    def test001_login(self,username,password,status_code,message,code):
        respones = self.login_api.login(username,password)
        print(respones.status_code)
        self.assertEqual(status_code,respones.status_code)
        self.assertEqual(message,respones.json().get('message'))
        self.assertEqual(code,respones.json().get('code'))


